//
//  CustomTabBarController.m
//  OCDemo
//
//  Created by zhumuzhi on 2021/3/18.
//

#import "CustomTabBarController.h"
// Controller
#import "HomeController.h"
#import "ClassificationController.h"
#import "ShopCartController.h"
#import "MineController.h"
// DBData
#import "DBBaseObject.h"

@interface CustomTabBarController ()<UITabBarControllerDelegate>

@end

@implementation CustomTabBarController

#pragma mark - LifeCycele

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    
    
    if (@available(iOS 15.0, *))  {
        UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
        appearance.backgroundColor = UIColorFromRGB(0xfafafa);
        self.tabBar.standardAppearance = appearance;
        self.tabBar.scrollEdgeAppearance = appearance;
    // 设置Tabbar背景颜色属性
    }else if (@available(iOS 13.0, *)) {
        UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
        appearance.shadowColor = [UIColor clearColor];
        appearance.backgroundColor = UIColorFromRGB(0xfafafa);
        [self.tabBar setStandardAppearance:appearance];
    }else {
        // 隐藏tabBar的顶部线
        self.tabBar.shadowImage = [UIImage new];
        self.tabBar.backgroundImage = [UIImage new];
        if ([self.tabBar respondsToSelector:@selector(setBarTintColor:)]) {
            self.tabBar.barTintColor = UIColorFromRGB(0xfafafa);
        }
    }
    
    [[UITabBar appearance] setTranslucent:YES];    // view是否会延伸到最底部
    self.tabBar.tintColor = PUBLIC_ORANGE_COLOR;  // 设置Item选中颜色
    
    [self setupController];
    
    // ============== 打开数据库并新建表 ==============
    [[DBBaseObject sharedInstance] addDatabaseAndTableMethod];
    
}

/**
 * 设置Tabbar子控制器
 */
- (void)setupController {
    HomeController *homeController = [[HomeController alloc] initWithNavigationType:BYNavigationTypeShow];
    [self addChildViewController:homeController title:@"首页" image:@"tabBar_home_normal" selectedImage:@"tabBar_home_selected"];
    
    ClassificationController * discoverController = [[ClassificationController alloc] initWithNavigationType:BYNavigationTypeShow];
    [self addChildViewController:discoverController title:@"分类" image:@"tabBar_category_normal" selectedImage:@"tabBar_category_selected"];
    
    ShopCartController * messageController = [[ShopCartController alloc] initWithNavigationType:BYNavigationTypeShow];
    [self addChildViewController:messageController title:@"购物车" image:@"tabBar_shopcart_normal" selectedImage:@"tabBar_shopcart_selected"];
    
    MineController * mineController = [[MineController alloc] initWithNavigationType:BYNavigationTypeShow];
    [self addChildViewController:mineController title:@"我的" image:@"tabBar_mine_normal" selectedImage:@"tabBar_mine_selected"];
}

/**
 * 设置控制器方法
 * @param childController   TabbarItem控制器
 * @param title             标题
 * @param image             默认图片
 * @param selectedImage     选中图片
 */
- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
    
    childController.title = title;
    childController.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x333333)} forState:UIControlStateNormal];
    [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:PUBLIC_ORANGE_COLOR} forState:UIControlStateSelected];
    
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:childController];
    navigationController.navigationBar.barTintColor = [UIColor lightGrayColor];
    
    [self addChildViewController:navigationController];
}

@end
